package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.MyDiscoveryBean;
import com.beiing.tianshuai.tianshuai.util.DateUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;

/**
 * Created by Jinni on 2017/10/13 0013.
 */

public class MyDiscoverAdapter extends BaseRecyclerViewAdapter<MyDiscoveryBean.DataBean.NumberBean> {

    private Context mContext;
    private OnDelClick mOnDelClickListener;

    public MyDiscoverAdapter(Context context) {
        mContext = context;
    }

    public void setOnDelClickListener(OnDelClick onDelClickListener) {
        mOnDelClickListener = onDelClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_my_discover, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            final MyDiscoveryBean.DataBean.NumberBean discover = mList.get(position);
            ((ViewHolder) holder).mTvDiscoverTitle.setText(discover.getTitle());
            String createTime = DateUtil.timeStamp2Date(discover.getCreateTime(), "yyyy年MM月dd日");
            ((ViewHolder) holder).mTvDiscoverTime.setText(createTime);
            String cover = discover.getCover();
            if (!TextUtils.isEmpty(cover)) {
                String[] split = cover.split(",");
                RequestOptions requestOptions = new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .centerCrop()
                        .override(350, 350);
                Glide.with(mContext.getApplicationContext())
                        .load(TextUtils.isEmpty(cover) ? R.mipmap.default_avatar : HttpRequestConstant.FRESH_COVER_URL + split[0])
                        .apply(requestOptions)
                        .into(((ViewHolder) holder).mIvDiscoverCover);
            } else {
                ((ViewHolder) holder).mIvDiscoverCover.setVisibility(View.GONE);
            }
            ((ViewHolder) holder).mTvDiscoverDel.setOnClickListener(new BGAOnNoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    if (mOnDelClickListener != null) {
                        mOnDelClickListener.onDelClickListener(discover, holder.getAdapterPosition());
                    }
                }
            });
            ((ViewHolder) holder).itemView.setOnClickListener(new BGAOnNoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    if (mOnDelClickListener != null) {
                        mOnDelClickListener.onItemClickListener(discover, holder.getAdapterPosition());
                    }
                }
            });
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_discover_cover)
        ImageView mIvDiscoverCover;
        @BindView(R.id.tv_discover_title)
        TextView mTvDiscoverTitle;
        @BindView(R.id.tv_discover_time)
        TextView mTvDiscoverTime;
        @BindView(R.id.tv_discover_del)
        TextView mTvDiscoverDel;

        private View itemView;

        public ViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnDelClick{
        void onDelClickListener(MyDiscoveryBean.DataBean.NumberBean bean, int position);
        void onItemClickListener(MyDiscoveryBean.DataBean.NumberBean bean, int position);
    }
}
